<template>
  <div class="cart_page">
    <table>
      <thead>
        <tr>
          <th style="width: 8%">
            <i
             @click="totalSelectFn"
              :class="
                totalSelect
                  ? 'iconfont icon-yduifuxuankuangxuanzhong'
                  : 'iconfont icon-yduifuxuankuang'
              "
            ></i>
          </th>
          <th style="width: 30%">礼品信息</th>
          <th>兑换分数</th>
          <th>数量</th>
          <th>小计 (鸡腿)</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="item in arrData" :key="item.id">
          <td>
            <i
            @click="oneSelectFn(item)"
              :class="
                item.isSelected
                  ? 'iconfont icon-yduifuxuankuangxuanzhong'
                  : 'iconfont icon-yduifuxuankuang'
              "
            ></i>
          </td>
          <td>
            <section>
              <img
                width="84"
                :src="'http://sc.wolfcode.cn'+item.coverImg"
                alt="列表图片"
              />
              <div class="info">
                <h5>{{item.title}}</h5>
                <p>颜色、版本:{{item.versionDescription}}</p>
              </div>
            </section>
          </td>
          <td>{{item.coin}}鸡腿</td>
          <td>
            <div class="step">
              <span @click="reduceFn(item)">-</span>
              <input type="text" disabled v-model="item.total" />
              <span @click="addFn(item)">+</span>
            </div>
          </td>
          <td>{{item.totalCost}}鸡腿</td>
          <td>
            <span class="del" @click="DelFn(item.id)">删除</span>
          </td>
        </tr>
      </tbody>
    </table>
    <div class="total">总计：<span>{{totaoPrice}}鸡腿</span></div>
    <div class="submit">提交</div>
  </div>
</template>
<script>
import {CartDataApi,DeleteDataApi} from '../../request/api.js'
export default {
  computed:{
    totaoPrice(){
      //num 清零
      let num = 0
      //只要被选中，就用num累加计数器和与单价相乘
      this.arrData.map(item=>{
        if(item.isSelected){
          num += item.total * item.coin
        }
      })
      return num
    }
  },
  methods:{
    DelFn(id){
      DeleteDataApi(
        { id }
      ).then(res=>{
        if(res.code === 0){
          alert('删除成功')
          this.$router.go(0)
        }
      })
    },
    reduceFn(item){
      //总数等于1就返回
      if(item.total === 1){
        return
      }
      item.total--
    },
    addFn(item){
      item.total++
    },
   totalSelectFn(){
     //全部选中按钮的状态，
     this.totalSelect = !this.totalSelect
  
     //所有按钮的状态等于全选按钮的状态
     this.arrData.map(item=>{
       item.isSelected =this.totalSelect
     })
    //   //当全选按钮为true时
    //  if(this.totalSelect){
    //    //把数组中所有按钮的选中状态改为true
    //    this.arrData.map(item=>{
    //      item.isSelected = true
    //    })
    //  }else{
    //     //把数组中所有按钮的选中状态改为false
    //    this.arrData.map(item=>{
    //      item.isSelected = false
    //    })
    //  }
   },
   oneSelectFn(item){
     //单击按钮自己取反
     item.isSelected = !item.isSelected
    //计数
    let mynum = 0
    this.arrData.map((item)=>{
      //统计被选中按钮的个数
      if(item.isSelected){
        mynum++
      }
    })
    //如果所有按钮被选中
    if(mynum === this.arrData.length){
      //全选按钮为 true
      this.totalSelect = true
    } else{
      //全选按钮为false
      this.totalSelect = false
    }

   }
  },
  created(){
    CartDataApi().then((res)=>{
      if(res.code === 0){

        //深拷贝服务器返回的数据。
        let arr = JSON.parse(JSON.stringify(res.data))
        //给数组中的每一项设置复选框未选中。
        arr.map(item=>{
          item.isSelected = false
        })
        this.arrData = arr

      }
    })
  },
  data() {
    return {
      arrData:[],
      stepNum: 1,
      // 全选
      totalSelect: false,
      // 单选
      oneSelect: false,
    };
  },
};
</script>
<style lang="less" scoped>
.cart_page {
  background: #fff;
  table {
    width: 100%;
    border: 1px solid #e6e6e6;
    box-sizing: border-box;
    color: #666;
    border-collapse: collapse;
    font-size: 14px;
    thead {
      background: #f2f2f2;
      th {
        padding: 19px 0;
        .iconfont {
          cursor: pointer;
        }
        .icon-yduifuxuankuangxuanzhong {
          color: #0a328e;
        }
      }
    }
    tbody {
      tr {
        td {
          vertical-align: middle;
          text-align: center;
          padding: 19px 0;
          table-layout: fixed; // td的宽度固定，不随内容变化
          .iconfont {
            cursor: pointer;
          }
          .icon-yduifuxuankuangxuanzhong {
            color: #0a328e;
          }
          section {
            padding-left: 20px;
            display: flex;
            box-sizing: border-box;
            img {
              margin-right: 12px;
            }
            .info {
              padding-top: 20px;
              flex: 1;
              overflow: hidden;
              box-sizing: border-box;
              text-align: left;
              h5 {
                overflow: hidden;
                color: #333;
                font-size: 18px;
                white-space: nowrap;
                text-overflow: ellipsis;
                margin-bottom: 20px;
              }
              p {
                color: #666;
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
              }
            }
          }
          .step {
            width: 106px;
            height: 32px;
            margin: auto;
            span {
              float: left;
              width: 30px;
              height: 32px;
              display: block;
              border: solid 1px #d1d1d1;
              font-size: 20px;
              box-sizing: border-box;
              font-weight: normal;
              font-stretch: normal;
              line-height: 30px;
              letter-spacing: 0px;
              color: #999999;
              text-align: center;
              cursor: pointer;
              background: #fff;
            }
            input {
              box-sizing: border-box;
              width: 46px;
              height: 32px;
              float: left;
              text-align: center;
              font-size: 14px;
              line-height: 23px;
              letter-spacing: 0px;
              color: #666666;
              border: 0;
              border-top: 1px solid #d1d1d1;
              border-bottom: 1px solid #d1d1d1;
              background: #fff;
            }
          }
          .del {
            border: 1px solid #ececec;
            border-radius: 4px;
            padding: 5px 10px;
            cursor: pointer;
            &:hover {
              color: #fff;
              background: #0a328e;
            }
          }
        }
      }
    }
  }
  .total {
    padding: 30px 0;
    text-align: right;
    font-size: 22px;
    span {
      font-weight: bold;
      color: #fd604d;
    }
  }
  .submit {
    width: 175px;
    height: 40px;
    text-align: center;
    line-height: 40px;
    font-family: SourceHanSansSC-Light;
    font-size: 18px;
    font-weight: normal;
    font-stretch: normal;
    letter-spacing: 0px;
    color: #ffffff;
    cursor: pointer;
    background-color: #0a328e;
    float: right;
  }
}
</style>